package com.ruoyi.system.controller.basic;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.WareDto;
import com.ruoyi.system.domain.vo.PageInfo;
import com.ruoyi.system.domain.vo.UserVo;
import com.ruoyi.system.domain.vo.WareVo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.utils.MyPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.db.basic.BasicWarehouses;
import com.ruoyi.system.service.IBasicWarehousesService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 仓库管理Controller
 * 
 * @author sen
 * @date 2023-07-25
 */
@RestController
@RequestMapping("/basic/warehouses")
@Slf4j
public class BasicWarehousesController extends BaseController
{
    @Autowired
    private IBasicWarehousesService basicWarehousesService;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:warehouses:list')")
    @GetMapping("/list")
    public PageInfo<WareVo> list(BasicWarehouses basicWarehouses)
    {
        // startPage();
        List<WareDto> list = basicWarehousesService.selectWareDtoList(basicWarehouses);

        return MyPageUtils.startPage(list.stream().map(e -> {
            return basicWarehousesService.DtoConvertToVo(e);
        }).collect(Collectors.toList()));
    }

    /**
     * 导出仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:warehouses:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasicWarehouses basicWarehouses)
    {
        List<WareDto> list = basicWarehousesService.selectWareDtoList(basicWarehouses);
        ExcelUtil<WareVo> util = new ExcelUtil<WareVo>(WareVo.class);
        util.exportExcel(response, list.stream().map(e -> {
            return basicWarehousesService.DtoConvertToVo(e);
        }).collect(Collectors.toList()), "仓库管理数据");
    }

    /**
     * 获取仓库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:warehouses:query')")
    @GetMapping("")
    public AjaxResult getInfo(@RequestParam(value = "warehouseId",required = false) Long warehouseId, @RequestParam(value = "postId",required = true) Long postId)
    {
        List<UserVo> availableManagers = iSysUserService.getAvailableManagers(warehouseId, postId);
        WareVo wareVo = new WareVo();
        if(warehouseId != null)
        {
            WareDto wareDto = basicWarehousesService.selectBasicWarehousesByWarehouseId(warehouseId);
            wareVo = basicWarehousesService.DtoConvertToVo(wareDto);
        }
        wareVo.setManagers(availableManagers.stream().map(e ->{
            return new UserVo(e.getUserId(),e.getUserName(),e.getNickName());
        }).collect(Collectors.toList()));
        return success(wareVo);
    }

    /**
     * 新增仓库管理
     */
    @PreAuthorize("@ss.hasPermi('basic:warehouses:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicWarehouses basicWarehouses)
    {
        log.info("新增仓库管理,参数:{}",basicWarehouses);
        return toAjax(basicWarehousesService.insertBasicWarehouses(basicWarehouses));
    }

    /**
     * 修改仓库管理
     */
    @PreAuthorize("@ss.hasPermi('basic:warehouses:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicWarehouses basicWarehouses)
    {
        log.info("修改仓库管理,参数:{}",basicWarehouses);
        return toAjax(basicWarehousesService.updateBasicWarehouses(basicWarehouses));
    }

    /**
     * 删除仓库管理
     */
    @PreAuthorize("@ss.hasPermi('basic:warehouses:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{warehouseIds}")
    public AjaxResult remove(@PathVariable Long[] warehouseIds)
    {
        return toAjax(basicWarehousesService.deleteBasicWarehousesByWarehouseIds(warehouseIds));
    }

}
